package com.gongpingjialoan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.data.NewCarList;

import java.util.List;

public class NewCarSourceListAdapter extends BaseAdapter {


    private LayoutInflater mInflater;
    private Context context;
    public List<NewCarList.ResultsBean> mCarSourceList;


    public NewCarSourceListAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }


    public void setData(List<NewCarList.ResultsBean> CarSourceList) {
        this.mCarSourceList = CarSourceList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mCarSourceList == null) {
            return 0;
        }
        return mCarSourceList.size();
    }


    @Override
    public NewCarList.ResultsBean getItem(int position) {

        return mCarSourceList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final CarSourceViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.new_car_list_item, null);
            holder = new CarSourceViewHolder();
            holder.brandname = (TextView) convertView.findViewById(R.id.brand_name);
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.tvCarPrice = (TextView) convertView.findViewById(R.id.price);
            holder.guide_price_tv = (TextView) convertView.findViewById(R.id.guide_price_tv);
            holder.old_car_change_tv = (TextView) convertView.findViewById(R.id.old_car_change_tv);
            holder.official_price_tv = (TextView) convertView.findViewById(R.id.official_price_tv);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (CarSourceViewHolder) convertView.getTag(R.id.tag_first);
        }
        NewCarList.ResultsBean source = mCarSourceList.get(position);
        convertView.setTag(R.id.tag_second, source);
        holder.brandname.setText(source.getModel());
        holder.tvCarTitle.setText(source.getModel_detail());
        holder.guide_price_tv.setText(source.getConc_price()+"万");
        holder.tvCarPrice.setText(source.getLow_month_pay());
        holder.official_price_tv.setText("官方价"+source.getPrice()+"万");
        holder.official_price_tv.setPaintFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        if(source.isIs_displaced()){
            holder.old_car_change_tv.setVisibility(View.VISIBLE);
        }else{
            holder.old_car_change_tv.setVisibility(View.GONE);
        }
        Glide.with(context).load(source.getImage_url()).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);

        return convertView;

    }

    public class CarSourceViewHolder {
        public ImageView imgThumbnail;
        public TextView  brandname;
        public TextView tvCarTitle;

        public TextView tvCarPrice;
        public TextView guide_price_tv;
        public TextView old_car_change_tv;
        public TextView official_price_tv;

    }
}
